package com.xiaoyi.admin.controller;

import com.xiaoyi.admin.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

    @GetMapping(value = {"/", "/login"})
    public String loginPage() {
        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model) {
        if (user.getUsername() != null && user.getPassword() != null && user.getPassword().equals("1")) {
            // 将登录成功的用户报存起来
            session.setAttribute("loginUser", user);
            // 重定向到index页面，防止表单重复提交
            return "redirect:/index.html";
        }else {
            model.addAttribute("msg","用户名密码错误");
            // 回到登录页
            return "login";
        }

    }

    @GetMapping("/index.html")
    public String indexPage(HttpSession session, Model model) {
        // 判断是否登录
//        Object loginUser = session.getAttribute("loginUser");
//        if (loginUser != null){
//            return "index";
//        }else {
//            model.addAttribute("msg","请登录");
//            return "login";
//        }
        return "index";
    }

}
